package com.jiuyou.clientController.attached;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.attached.domain.Attached;
import com.ruoyi.attached.service.IAttachedService;
import com.ruoyi.attachedData.domain.AttachedData;
import com.ruoyi.attachedData.service.IAttachedDataService;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.jiuyou.clientController.common.CommonClientController;

import java.util.List;

/**
 * 附Controller
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/client/attached")
public class ClientAttachedController extends BaseController
{
    @Autowired
    private IAttachedService attachedService;

    @Autowired
    private IAttachedDataService attachedDataService;

    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private ISampleService sampleService;

    @Autowired
    private CommonClientController commonController;

    /**
     * 查询附列表(所有)
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(Attached attached)
    {
        List<Attached> list = attachedService.selectAttachedList(attached);
        return AjaxResult.success(list);
    }

    /**
     * 选择附件id查出它所有的内容
     */
    @GetMapping("/attachedDataAll")
    public AjaxResult attachedDataAll(Long attachedId)
    {
        AttachedData attachedData = new AttachedData();
        attachedData.setAttachedId(attachedId);
        List<AttachedData> attachedDataList = attachedDataService.selectAttachedDataList(attachedData);
        return AjaxResult.success(attachedDataList);
    }

    /**
     * 查询附列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Attached attached)
    {
        startPage();
        List<Attached> list = attachedService.selectAttachedList(attached);
        return getDataTable(list);
    }

    /**
     * 导出附列表
     */
    @Log(title = "附", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Attached attached)
    {
        List<Attached> list = attachedService.selectAttachedList(attached);
        ExcelUtil<Attached> util = new ExcelUtil<Attached>(Attached.class);
        return util.exportExcel(list, "附数据");
    }

    /**
     * 获取附详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Attached attached = attachedService.selectAttachedById(id);

        Commission commission_one =  commissionService.selectCommissionById(attached.getCommissionId());
        attached.setCommissionOne(commission_one);
        String businessType = commission_one.getBusinessType();
        attached.setBusinessType(businessType);

        if(businessType.equals("shinei")){
            //查询样品表 sample
            Sample sample = new Sample();
            sample.setAttachedId(id);
            List<Sample> sampleList = sampleService.selectSampleList(sample);
            attached.setSampleList(sampleList);
        }

        if(businessType.equals("shiwai")){
//            System.out.println("tangyin999");
            AttachedData attachedData = new AttachedData();
            attachedData.setAttachedId(id);
            List<AttachedData> attachedDataList = attachedDataService.selectAttachedDataList(attachedData);
            attached.setAttachedDataList(attachedDataList);
        }
        attached.setBusinessType(commission_one.getBusinessType());
        attached.setType(commission_one.getType());
        return AjaxResult.success(attached);
    }

    /**
     * 新增附
     */
    @Log(title = "附", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Attached attached)
    {
        Long commissionId = attached.getCommissionId();
        Commission c_one = commissionService.selectCommissionById(commissionId);
        attached.setEngineeringId(c_one.getEngineeringId());
        attached.setEnterpriseId(c_one.getEnterpriseId());
        return toAjax(attachedService.insertAttached(attached));
    }

    /**
     * 修改附
     */
    @Log(title = "附", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attached attached)
    {
        attached.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(attachedService.updateAttached(attached));
    }

    /**
     * 删除附
     */
    @Log(title = "附", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attachedService.logicDeleteAttached(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }

    /**
     * 导入表格
     * */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long commissionId,Long type) throws Exception
    {
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        Commission c_one = commissionService.selectCommissionById(commissionId);

        Attached attached = new Attached();
        attached.setName(originalFilename);
        attached.setCommissionId(commissionId);
        attached.setEngineeringId(c_one.getEngineeringId());
        attached.setEnterpriseId(c_one.getEnterpriseId());
        attachedService.insertAttached(attached);

        //存入BusinessType
        Commission commission = new Commission();
        commission.setId(commissionId);
        commission.setBusinessType("shiwai");
        commission.setType(type);
        commissionService.updateCommission(commission);

        Long attachedId = attached.getId();

        ExcelUtil<AttachedData> util = new ExcelUtil<AttachedData>(AttachedData.class);
        List<AttachedData> attachedDataList = util.importExcel(file.getInputStream());
        System.out.println(attachedDataList);
        String operName = getUsername();
        String message = attachedDataService.importAttachedData(attachedDataList,attachedId,commissionId,c_one.getEngineeringId(),type);
        return AjaxResult.success(message);
    }


    /**
     * 附表上传（室内检测）
     * */
    @Log(title = "附", businessType = BusinessType.INSERT)
    @PostMapping("typeAdd")
    public AjaxResult typeAdd(@RequestBody Attached attached)
    {
        Long commissionId = attached.getCommissionId();
        Commission c_one = commissionService.selectCommissionById(commissionId);
        Long engineeringId = c_one.getEngineeringId();
        Long enterpriseId = c_one.getEnterpriseId();

        attached.setEnterpriseId(enterpriseId);
        attached.setEngineeringId(engineeringId);
        Integer is_attached = attachedService.insertAttached(attached);
        Long attachedId = attached.getId();

        String businessType = attached.getBusinessType();

        //存入BusinessType
        Commission commission = new Commission();
        commission.setId(commissionId);
        if(StringUtils.isNotEmpty(businessType)) {
            commission.setBusinessType(businessType);
        }
        commissionService.updateCommission(commission);

//        if(businessType.equals("shinei")){
            //土样数量不为空
            if(attached.getSoilSample() != null) {
                Sample sample = new Sample();
                sample.setCommissionId(commissionId);
                sample.setEngineeringId(engineeringId);
                sample.setCategory("shinei");
                sample.setProject("tuyang");
                sample.setNumber(attached.getSoilSample());
                sample.setUnit("组");
                sample.setAttachedId(attachedId);
                sampleService.insertSample(sample);
            }
            //水样
            if(attached.getWaterSample() != null) {
                Sample sample = new Sample();
                sample.setCommissionId(commissionId);
                sample.setEngineeringId(engineeringId);
                sample.setCategory("shinei");
                sample.setProject("shuiyang");
                sample.setNumber(attached.getWaterSample());
                sample.setUnit("组");
                sample.setAttachedId(attachedId);
                sampleService.insertSample(sample);
            }
            //岩样
            if(attached.getRockSample() != null) {
                Sample sample = new Sample();
                sample.setCommissionId(commissionId);
                sample.setEngineeringId(engineeringId);
                sample.setCategory("shinei");
                sample.setProject("yanyang");
                sample.setNumber(attached.getRockSample());
                sample.setUnit("组");
                sample.setAttachedId(attachedId);
                sampleService.insertSample(sample);
            }
            //土腐蚀
            if(attached.getSoilCorrosion() != null) {
                Sample sample = new Sample();
                sample.setCommissionId(commissionId);
                sample.setEngineeringId(engineeringId);
                sample.setCategory("shinei");
                sample.setProject("tufushi");
                sample.setNumber(attached.getSoilCorrosion());
                sample.setUnit("组");
                sample.setAttachedId(attachedId);
                sampleService.insertSample(sample);
            }
//        }
        return AjaxResult.success(is_attached);
    }

    /**
     * 导入表格(室内)
     * */
//    @PreAuthorize("@ss.hasPermi('attached:attached:import')")
    @PostMapping("/importDataNie")
    public AjaxResult importDataNie(MultipartFile file) throws Exception
    {
        List<List<Object>> data = ExcelUtil.readExcel(file);

        Long tuyang_num = 0L;
        Long shuiyang_num = 0L;
        Long tufushi_num = 0L;
        Long yanyang_num = 0L;

        if(data.get(5).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(5).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(6).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(6).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(7).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(7).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(8).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(8).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(9).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(9).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(10).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(10).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(11).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(11).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(12).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(12).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(13).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(13).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        if(data.get(14).get(3).equals("√")){tuyang_num = tuyang_num +1;}
        if(data.get(14).get(4).equals("√")){tuyang_num = tuyang_num +1;}

        //水样统计
        if(data.get(15).get(29).equals("√")){shuiyang_num = shuiyang_num +1;}
        if(data.get(16).get(29).equals("√")){shuiyang_num = shuiyang_num +1;}

        //土腐蚀统计
        if(data.get(17).get(30).equals("√")){tufushi_num = tufushi_num +1;}

        //岩样
        if(data.get(18).get(31).equals("√")){yanyang_num = yanyang_num +1;}
        if(data.get(18).get(32).equals("√")){yanyang_num = yanyang_num +1;}
        if(data.get(18).get(33).equals("√")){yanyang_num = yanyang_num +1;}
        if(data.get(18).get(34).equals("√")){yanyang_num = yanyang_num +1;}

        JSONObject jsonObject = new JSONObject();

        JSONObject json = new JSONObject();
        json.put("tuyang_num", tuyang_num);
        json.put("shuiyang_num", shuiyang_num);
        json.put("tufushi_num", tufushi_num);
        json.put("yanyang_num", yanyang_num);

        AjaxResult ajax = commonController.uploadFile(file);
        jsonObject.put("fileInfo",ajax);
        jsonObject.put("sampleList",json);

        return AjaxResult.success(jsonObject);
    }


}
